home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Component Debug ƒ / IC Resource ƒ / Syslog Component ƒ / Libsprintf.c < prev    next >
Encoding:
Text File  |  1995-11-15  |  13.2 KB  |  203 lines  |  [TEXT/SPM ]

  1.                     D.min = D.dot;
  2.                 }
  3.                 if (!F.altForm && D.max > D.dec.sig.length) {
  4.                     if ((D.max = D.dec.sig.length) < D.dot)
  5.                         D.max = D.dot;
  6.                 }
  7.                 goto floating;
  8.                 
  9.                     /*  floating (common code)  */
  10.  
  11. floating:
  12.                 if (D.dec.sig.text[0] > '9') {
  13.                     F.exponent = FALSE;
  14.                     D.dot = 0;
  15.                     D.min = 1;
  16.                     D.max = D.dec.sig.length;
  17.                 }
  18.                 i = 0;
  19.                 if (F.exponent) {
  20.                     n = D.dec.exp < 0 ? -D.dec.exp : D.dec.exp;
  21.                     for (; n; n /= 10, i++)
  22.                         *--s = n % 10 + '0';
  23.                     for (; i < 2; i++)
  24.                         *--s = '0';
  25.                     *--s = D.dec.exp < 0 ? '-' : '+';
  26.                     *--s = F.exponent;
  27.                     i += 2;
  28.                 }
  29.                 j = D.max;
  30.                 if (j == D.dot && !F.altForm)
  31.                     ++D.dot;
  32.                 do {
  33.                     if (j == D.dot) {
  34.                         *--s = '.';
  35.                         i++;
  36.                     }
  37.                     *--s = j > 0 && j <= D.dec.sig.length ? D.dec.sig.text[j-1] : '0';
  38.                 } while (--j >= D.min);
  39.                 i += D.max - j;
  40.                 if (D.dec.sgn)
  41.                     F.sign = '-';
  42.                 else if (F.forceSign)
  43.                     F.sign = '+';
  44.                 if (F.zeroPad) {
  45.                     j = F.fieldWidth;
  46.                     if (F.sign)
  47.                         --j;
  48.                     if (i<j){
  49.                         s -= (j-i);
  50.                         
  51.                         BlockMoveData((Ptr)__gZeroString,(Ptr)s,(j-i));
  52.                         i=j;
  53.                     }
  54.                 }
  55.                 if (F.sign) {
  56.                     *--s = F.sign;
  57.                     i++;
  58.                 }
  59.                 break;
  60. #endif /* _NOFLOATING_ */
  61.                 /*  store # bytes written so far  */
  62.                 
  63.             case 'n':
  64.                 s = (char *)va_arg(arg, void *);
  65.                 if (F.hSize)
  66.                     * (short *) s = nwritten;
  67.                 else if (F.lSize)
  68.                     * (long *) s = nwritten;
  69.                 else
  70.                     * (int *) s = nwritten;
  71.                 continue;
  72.             
  73.                 /*  oops - unknown conversion, abort  */
  74.                 
  75.             default:
  76.                 if (c != '%')
  77.                     goto done;
  78.             copy1:
  79.                 *fp=c;fp++;
  80.                 ++nwritten;
  81.                 continue;
  82.         }
  83.             
  84. EndOfSwitch:
  85.             /*  pad on the left  */
  86.             
  87.         if (i < F.fieldWidth && !F.leftJustify) {
  88.             j=F.fieldWidth-i;
  89.             
  90.             BlockMoveData((Ptr)__gSpaceString,(Ptr)fp,j);
  91.             
  92.             fp+=j;
  93.             nwritten+=j;
  94.             F.fieldWidth=0;
  95.         }
  96.         
  97.             /*  write the converted result  */
  98.         
  99.         BlockMoveData((Ptr)s,(Ptr)fp,i);
  100.         fp+=i;
  101.         nwritten += i;
  102.             
  103.             /*  pad on the right  */
  104.             
  105.         if (i<F.fieldWidth){
  106.             j=F.fieldWidth-i;
  107.             
  108.             BlockMoveData((Ptr)__gSpaceString,(Ptr)fp,j);
  109.             fp+=j;
  110.             nwritten+=j;
  111.         }
  112.     }
  113.     
  114.         /*  all done!  */
  115.         
  116. done:
  117.     *fp=0;
  118.     return nwritten;
  119. }
  120.  
  121. #ifndef _NOFLOATING_
  122.  
  123. static void post_conversion(LibDecRec *d){
  124.     int i;
  125.     /*  strip trailing zeroes  */
  126.  
  127.     for (i = d->dec.sig.length; i > 1 && d->dec.sig.text[i-1] == '0'; --i)
  128.         ++d->dec.exp;
  129.     d->dec.sig.length = i;
  130.         
  131.         /*  format 0, INF, NAN  */
  132.         
  133.     if (d->dec.sig.text[0] == '0') {
  134. /*        d->dec.sgn = 0;             add back to disallow printing "-0"  */
  135.         d->dec.exp = 0;
  136.     }
  137.     else if (d->dec.sig.text[0] == 'I') {
  138.         d->dec.sig.length = 3;
  139.         d->dec.sig.text[1] = 'N';
  140.         d->dec.sig.text[2] = 'F';
  141.     }
  142.     else if (d->dec.sig.text[0] == 'N') {
  143.         d->dec.sig.length = 5;
  144.         d->dec.sig.text[1] = 'A';
  145.         d->dec.sig.text[2] = 'N';
  146.     }
  147. }
  148.  
  149. /* ---------- floating-point conversion ---------- */
  150.  
  151. #if powerc
  152. static void libf2d( int fixed, int precision, LibDecRec *d, long double x){
  153.     struct decform form;
  154.     
  155.     if (precision >= sizeof d->dec.sig)
  156.         precision = sizeof d->dec.sig - 1;
  157.  
  158.     if (fixed)
  159.         form.style = FIXEDDECIMAL;
  160.     else
  161.         form.style = FLOATDECIMAL;
  162.     form.digits = precision;
  163.     /*
  164.         Until the long double data type is really implemented, this must be a call
  165.         to num2dec and not num2decl even though the argument is declared as a long
  166.         double 
  167.     */
  168.     
  169.     num2dec( &form, x, &(d->dec) );
  170.     
  171.         /*  handle fixed-point overflow  */
  172.         
  173.     if (d->dec.sig.text[0] == '?') {
  174.         form.style = FLOATDECIMAL;
  175.         form.digits = sizeof d->dec.sig - 1;
  176.         num2dec( &form, x, &(d->dec));
  177.     }
  178.     post_conversion(d);
  179. #else
  180.  
  181. static void libf2d(int fixed, int precision, LibDecRec *d, long double x){
  182.     struct { char style; short digits; } form;
  183.     REGISTER short *p = (short *) &x;
  184.     
  185.         /*  point to SANE extended  */
  186.  
  187.     #if __option(mc68881) && _NATIVE_FP_
  188.         p[1] = p[0];
  189.     #endif
  190.     #if __option(mc68881) || !_NATIVE_FP_
  191.         p++;
  192.     #endif
  193.     
  194.         /*  convert to decimal record  */
  195.     
  196.     if (precision >= sizeof d->dec.sig)
  197.         precision = sizeof d->dec.sig - 1;
  198.     form.style = fixed;
  199.     form.digits = precision;
  200.     fp68k(&form, p, d, FX2DEC);
  201.     
  202.         /*  handle fixed-point overflow  *